#include <QTimer>
#include "cutdown.h"
#include "global/global.h"
#define THEME_SCHAME "org.ukui.style"

using namespace kdk;
CutDown::CutDown(QWidget *parent, int cutdown) : QWidget(parent), m_cutdown(cutdown)
{
    const QByteArray style_id(THEME_SCHAME);
    if(QGSettings::isSchemaInstalled(style_id)){
        m_styleSettings = new QGSettings(style_id);
    }
    this->setAttribute(Qt::WA_AlwaysShowToolTips);
    this->setCursor(Qt::ArrowCursor);

    initLayout();
    initConnect();
    initFontSize();
    initGsetting();
    installEventFilter(this);

    m_timer = new QTimer( this );
    m_timer->setTimerType( Qt::PreciseTimer );
    connect( m_timer, SIGNAL(timeout()), this, SLOT(slotUpdateTimer()));
    m_TimeRec->setText(QString("%1s").arg(m_cutdown));
    m_timer->start(1000);
}

bool CutDown::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::ToolTip) {
        // 阻止工具提示事件传播
        return true;
    }

    return QWidget::eventFilter(obj, event);
}

void CutDown::initFontSize()
{
    //只有非标准字号的控件才需要绑定
    connect(this, &CutDown::setFontSizeSignal, this, &CutDown::lable2SetFontSizeSlot);
    connect(m_styleSettings,&QGSettings::changed,this,[=](const QString &key) {
        if (key == "systemFontSize" || key == "systemFont") {
            QString fontStyle = m_styleSettings->get("systemFont").toString();
            int fontSizeKey = m_styleSettings->get("systemFontSize").toString().toInt();
            if (fontSizeKey > 0) {
                if (fontSizeKey > 15) {
                    fontSizeKey = 15;
                }
                Q_EMIT setFontSizeSignal(fontSizeKey);
            }
        }
    });
    //启动时设置字号
    int fontSizeKey = 13; //系统默认字号
//    int fontSizeKeyTmp = kdk::kabase::Gsettings::getSystemFontSize().toInt();
//    if (fontSizeKeyTmp > 0) {
//        fontSizeKey = fontSizeKeyTmp;
//    }
    if (fontSizeKey > 15) {
        fontSizeKey = 15;
    }
    Q_EMIT setFontSizeSignal(fontSizeKey);
}

void CutDown::lable2SetFontSizeSlot(int size)
{
    //默认大小12px,换算成pt为9
    double lable2BaseFontSize = DEFAULT_FONT_SIZE;
    double nowFontSize = lable2BaseFontSize * double(size) / DEFAULT_FONT_SIZE; // 11为系统默认大小
    QFont font;
    font.setPointSizeF(nowFontSize);
    font.setBold(true);
    m_TimeRec->setFont(font);
}

//监听主题
void CutDown::initGsetting()
{
    connect(m_styleSettings, &QGSettings::changed,this,[=](const QString &key) {
        if ("themeColor" == key || "styleName" == key) {
            changeStyle();
        }
    });
    changeStyle();
}

void CutDown::changeStyle()
{
    //需要重新梳理此处的QSS
    nowThemeStyle = m_styleSettings->get("styleName").toString();
    if ("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle) {
        this->setStyleSheet("QWidget{border-radius:" + widRadius + ";border:none;color:rgb(255,255,255);}");
        g_tooleWid->setStyleSheet("QWidget{background-color:rgba(0,0,0,0.66);border-radius:" + widRadius
                                  + ";border:none;}");
//        m_line1->setStyleSheet("QLabel{border: 1px solid #727272;background-color: #727272; opacity: 1}");
    } else {
        this->setStyleSheet("QWidget{border-radius:" + widRadius + ";border:none;color:rgb(0,0,0);}");
        g_tooleWid->setStyleSheet("QWidget{background-color:rgba(255,255,255,1);border:none;border-radius:" + widRadius + ";color:rgb(0,0,0);}");
//        m_line1->setStyleSheet("QLabel{border: 1px solid #393939;background-color: #393939; opacity: 1}");
//        m_btnCancelRec->setStyleSheet("QPushButton{background:rgba(255,255,255,1);font:12pt 微软雅黑 blod;}"
//                      "QPushButton:hover{color:red;}"
//                      "QPushButton:pressed{color:blue}");
//        m_btnCancelRec->setStyleSheet("QToolButton{background-color:rgba(255,255,255,1); QToolButton:hover{color:rgb(255,0,0);}");//
    }
}

void CutDown::slotCancelRec()
{
    m_timer->stop();
    emit sigCancelRec();
}

void CutDown::slotUpdateTimer()
{
    m_cutdown--;
    if (m_cutdown == 0)
    {
        m_timer->stop();
        emit sigStartRec(m_cutdown);
    }
    else
    {
        m_TimeRec->setText(QString("%1s").arg(m_cutdown));
    }
}

void CutDown::initLayout()
{
    //X11窗口装饰
    MotifWmHints hints1;
    hints1.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
    hints1.functions = MWM_FUNC_ALL;
    hints1.decorations = MWM_DECOR_BORDER;
    XAtomHelper::getInstance()->setWindowMotifHint(winId(), hints1);

    this->setFixedSize(141, 56);
    g_tooleWid = new QFrame(this);
    QHBoxLayout *m_layout = new QHBoxLayout(this);
    m_line1 = new QLabel(this);
    m_line1->setStyleSheet("QLabel{border: 1px solid #c5cacb;background-color: #c5cacb;}");
    m_TimeRec = new QLabel(this);
    m_TimeRec->setContentsMargins(0,0,0,0);
    m_btnCancelRec = new kdk::KBorderlessButton(this);
    m_btnCancelRec->setAttribute(Qt::WA_TranslucentBackground);
    m_btnCancelRec->setText(tr("cancel"));

    m_layout->addSpacing(15);
    m_layout->addWidget(m_TimeRec, 0, Qt::AlignRight);
    m_layout->addWidget(m_line1, 0, Qt::AlignCenter);
    m_layout->addWidget(m_btnCancelRec, 0, Qt::AlignCenter);
    m_layout->addSpacing(10);
    g_tooleWid->setLayout(m_layout);
    g_tooleWid->move(2, 2);
    g_tooleWid->resize(this->width() - 4, this->height() - 4);

    //绘制阴影
    QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect(this);
    effect->setOffset(0, 0); //设置向哪个方向产生阴影效果(dx,dy)，特别地，(0,0)代表向四周发散
    effect->setColor(TOOL_COLOR);       //设置阴影颜色，也可以setColor(QColor(220,220,220))
    effect->setBlurRadius(BLUR_RADIUS); //设定阴影的模糊半径，数值越大越模糊
    g_tooleWid->setGraphicsEffect(effect);

    setBtnSize();
    setBtnIconSize();
    setBtnIcon();
    setTooltips();
}

void CutDown::initConnect()
{
    connect(m_btnCancelRec, &QPushButton::clicked, this,&CutDown::slotCancelRec);
}

void CutDown::setBtnSize()
{
    m_TimeRec->setFixedSize(30, 36);
    m_line1->setFixedSize(QSize(1, 24));
    m_btnCancelRec->setFixedSize(38, 30);
}

void CutDown::setBtnIconSize()
{
    m_btnCancelRec->setIconSize(QSize(28, 20));
}

void CutDown::setBtnIcon()
{
    //m_btnCancelRec->setIcon(QIcon(":/icon/cancelrec.svg"));

}

void CutDown::setTooltips()
{
//    m_btnCancelRec->setToolTip(tr("取消录屏"));
//    this->setToolTip("此工具条不会被录制，按住亦可拖拽");
}

